//
//  SortNavigation.m
//  Remind
//
//  Created by mac on 17/9/29.
//  Copyright © 2017年 2017.09.27. All rights reserved.
//

#import "SortNavigation.h"
#define SORTBTNTAG 3000
#define MAINCOLOR [UIColor colorWithRed:20/255.0 green:150/255.0 blue:200/255.0 alpha:1.0]
@interface SortNavigation ()
{
    NSArray<NSString *> *titleArr;
    NSMutableArray<UIButton *> *btnArr;
    NSInteger btnWidth;
    
    UIColor *titleColor;
    UIColor *selectTitleColor;
    UIColor *backGroundColor;
    UIColor *selectBGColor;
}


@end

@implementation SortNavigation
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}

- (instancetype)initWithFrame:(CGRect)frame Titles:(NSArray<NSString *> *)titles DefaultIndex:(NSInteger)idx{
    selectTitleColor = [UIColor colorWithRed:20/255.0 green:150/255.0 blue:200/255.0 alpha:1.0];
    titleColor = [UIColor grayColor];
    selectBGColor = [UIColor colorWithWhite:0.8 alpha:0.6];
    backGroundColor = [UIColor whiteColor];
    
    titleArr = [NSArray arrayWithArray:titles];
    btnArr = [NSMutableArray new];
    
    _currIDX = idx;
    if(titles.count > 4){
        CGFloat width =  ceil(frame.size.width / 5);
        btnWidth = ceil(width/8) + width;
        NSLog(@"%ld",(long)btnWidth);
    }else btnWidth = ceil(frame.size.width / titleArr.count);
    
    self = [self initWithFrame:frame];
    if(self){
        int i = 0;
        for(NSString *str in titleArr){
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(btnWidth * i, 0, btnWidth, self.mainScrollView.frame.size.height);
            btn.tag = SORTBTNTAG + i;
            [btn setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15]];
            [btn setTitle:str forState:UIControlStateNormal];
            
            if(i == _currIDX) {
                [btn setTitleColor:selectTitleColor forState:UIControlStateNormal];
                btn.backgroundColor = selectBGColor;
            } else {
                [btn setTitleColor:titleColor forState:UIControlStateNormal];
                btn.backgroundColor = backGroundColor;
            };
            
           
            //btn.titleLabel.adjustsFontSizeToFitWidth = YES;
            [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
            
            [btnArr addObject:btn];
            [self.mainScrollView addSubview:btn];
            i++;
        }

        //NSLog(@"%@",self.mainScrollView.subviews);
        _bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 2, self.frame.size.width, 2)];
        _bottomLine.backgroundColor = MAINCOLOR;
        [self addSubview:_bottomLine];
        
        _moveLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 5, btnWidth, 5)];
        _moveLine.backgroundColor = MAINCOLOR;
        [self.mainScrollView addSubview:_moveLine];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor blueColor];
        self.mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.mainScrollView.showsVerticalScrollIndicator = NO;
        self.mainScrollView.showsHorizontalScrollIndicator = NO;
        self.mainScrollView.userInteractionEnabled = YES;
        self.mainScrollView.backgroundColor = [UIColor whiteColor];
        CGFloat count = titleArr.count;
        self.mainScrollView.contentSize = CGSizeMake(count * btnWidth, 0);
        [self addSubview:[UIView new]];
        [self addSubview:self.mainScrollView];
        //NSLog(@"%@",self.subviews);
    }
    return self;
}

- (void)setButtonBackgroundColor:(UIColor * __nonnull)bgColor SelectBGColor:(UIColor * __nonnull)sBGColor TitleColor:(UIColor * __nonnull)tColor SelectTitleColor:(UIColor * __nonnull)sTColor{
    backGroundColor = bgColor;
    selectBGColor = sBGColor;
    titleColor = tColor;
    selectTitleColor = sTColor;
    [self setCurrentButtonInIndex:_currIDX];
}

//根据传入的idx设置当前选中的按钮颜色
- (void)setCurrentButtonInIndex:(NSInteger)idx{
    
    UIButton *currentBtn = btnArr[idx];
    for(UIButton *btn in btnArr){
        [btn setTitleColor:titleColor forState:UIControlStateNormal];
        btn.backgroundColor = backGroundColor;
    }
    [currentBtn setTitleColor:selectTitleColor forState:UIControlStateNormal];
    currentBtn.backgroundColor = selectBGColor;
}

- (void)setScrollViewOffsetXByIndex:(NSInteger)idx{
    [self setCurrentButtonInIndex:idx];
    
    [UIView transitionWithView:_moveLine duration:0.5 options:UIViewAnimationOptionTransitionNone animations:^{
        _moveLine.center = CGPointMake(idx * btnWidth + btnWidth/2, _moveLine.frame.origin.y + _moveLine.frame.size.height / 2);
        
    } completion:nil];
    
    NSInteger X = 0;
  
//    if(idx > 3 && idx != titleArr.count - 1){
//        X = (idx - 3)*btnWidth;
//    }else if(idx == titleArr.count - 1 && idx > 3){
//        NSInteger offsetX = btnWidth - (self.frame.size.width - btnWidth*4);
//        NSLog(@"offsetX:%d",offsetX);
//        X = (idx - 3)*btnWidth - offsetX;
//    }
    if(idx > 3){
        X = (idx - 3)*btnWidth;
        if(idx == titleArr.count - 1&&titleArr.count > 4){
            
            X = btnWidth * titleArr.count - ((self.frame.size.width - btnWidth * 4) + btnWidth * 4);
        }
    }
    
    [self.mainScrollView setContentOffset:CGPointMake(X, 0) animated:YES];
    
}

- (void)onClick:(UIButton *)sender{
    NSInteger index = sender.tag - SORTBTNTAG;
    _currIDX = index;
    if(self.clickBlock){
        self.clickBlock(index);
    } else [self.delegate selectButtonAtIndex:index];
    [self setScrollViewOffsetXByIndex:index];
    
}

@end
